// miniprogram/pages/new/new.js
const app = getApp()
const db = wx.cloud.database()
import Notify from '@vant/weapp/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    typeList:[{name:'积分卡',type:1},{name:'美睫毛',type:2},{name:'美甲卡',type:3},{name:'激光脱毛',type:4},{name:'充值卡',type:5}],
    typeName:'请选择类型',
    type:'',
    form:{
      typeName:'',
      type:'',
      name:'',
      tel:'',
      number:'',
      remark:'',
      date:''
    },
  },
  bindDateChange: function(e) {
    this.setData({
      date:e.detail.value
    })
  },
  bindchange(e){
    let index = e.detail.value
    let typeName = this.data.typeList[index].name
    let type = this.data.typeList[index].type
    this.setData({
      typeName,
      type
    })
  },
  formSubmit(e) {
    if(!this.data.typeName||this.data.typeName=='请选择类型'){
      wx.showToast({
        title: '卡的类型不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!e.detail.value.name){
      wx.showToast({
        title: '客户名称不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    
    let form = e.detail.value
    form['typeName'] = this.data.typeName
    form['type'] = this.data.type
    this.setData({
      form
    })
    console.log(form)
    let _that = this;
    if(!_that.data.id){//新增
      db.collection('customerList').add({
        data:{
          ...form
        }
      }).then(res=>{
        if(res.errMsg=='collection.add:ok'){
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 1000,
            success:function(){
              _that.getData()
            }
          })
        }
      }).catch(res=>{
        console.log(res)
        wx.showToast({
          title: '添加失败',
          icon: 'error',
          duration: 1000
        })
      })
    }else{//修改
      wx.cloud.callFunction({
        name:'update',
        data:{id:_that.data.id,form:form}
      }).then(res=>{
        wx.showToast({
          title: '更新成功',
          icon: 'success',
          duration: 1000,
          success:function(){
            _that.getData()
          }
        })
      })
      // db.collection('customerList').doc(_that.data.id).update({
      //   data: {
      //     ...form
      //   }
      // }).then(res=>{
      //   if(res.errMsg=='document.update:ok'){
      //     wx.showToast({
      //       title: '更新成功',
      //       icon: 'success',
      //       duration: 2000,
      //       success:function(){
      //         wx.navigateTo({
      //           url: '/pages/index/index',
      //         })
      //       }
      //     })
      //   }
      // })
    }
  },
  delete(){
    let _that = this;
    wx.showModal({
      title:"删除",
      content:'确定删除吗？',
      success(res){
        if(res.confirm){
          console.log('用户点击确定')
          wx.cloud.callFunction({
            name:'delete',
            data:{id:_that.data.id}
          }).then(res=>{
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 1000,
              success:function(){
                _that.getData()
              }
            })
          })
        }else if(res.cancel){
          console.log('用户点击取消')
        }
      }
    })
    // db.collection('customerList').doc(this.data.id).remove().then(res=>{
    //   if(res.errMsg=='document.remove:ok'){
    //     wx.showToast({
    //       title: '删除成功',
    //       icon: 'success',
    //       duration: 2000,
    //       success:function(){
    //         wx.navigateTo({
    //           url: '/pages/index/index',
    //         })
    //       }
    //     })
    //   }
    // }).catch(error=>{
    //   wx.showToast({
    //     title: '删除失败',
    //     icon: 'success',
    //     duration: 2000
    //   })
    // })
  },
  //重新拉数据
  getData(){
    let _that = this;
    wx.cloud.callFunction({
      name:'updateInfo',
      success:res=>{
        let data = res.result
        app.globalData = data
        wx.setStorageSync('customerData', data)
        wx.navigateBack()
        // wx.reLaunch({
        //   url: '/pages/index/index?type='+_that.data.type,
        // })
      },
      fail:err=>{
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    if(this.data.id){
      let _that = this;
      db.collection('customerList').doc(this.data.id).get({
        success:function(res){
          console.log("-----------------------")
          console.log(res)
          _that.setData({
            form:res.data,
            date:res.data.date,
            typeName:res.data.typeName,
            type:res.data.type
          })
        }
      })
    }
  },
  resetData(){
    let _that = this;
    wx.showModal({
      title:"清空数据，请谨慎操作！",
      content:'您确定清空积分卡所有人的消费日期、消费次数、备注的信息吗？',
      success(res){
        if(res.confirm){
          wx.showLoading({
            title: '处理中请稍等...',
            mask:true
          })
          //先备份数据，再清空积分卡类型的消费日期、消费次数、备注的信息操作
          wx.cloud.callFunction({
            name:'backup',
            success:res=>{
              wx.hideLoading()
              Notify({ type: 'success', message: '更新完成请查看！' });
              _that.getData()
              console.log(res)
            },
            fail:err=>{
              wx.hideLoading()
              Notify({ type: 'danger', message: '更新失败请重试' });
              console.log(err)
            }
          })
        }else if(res.cancel){
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})